package com.touzbi.hss.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.touzbi.hibernate.util.HibernateUtil;
import com.touzbi.hss.beans.Patient;
import com.touzbi.hss.bo.PatientListBO;

/**
 * Servlet implementation class Patientservlet
 */
@WebServlet("/patient-list")
public class PatientListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String PATIENT_JSP = "/WEB-INF/list.jsp";
	private static final String LIST_PATIENT_NAMES_ATT = "listPatient";
	private static final String LIST_COLUMN_ATT = "listColumn";
	private static final String LIST_TITLE_ATT = "title";
	private static final Object LIST_TITLE = "List Patient";

	private static final String PATIENT_LIST_ATT = "search";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		PatientListBO patientBO = new PatientListBO(session);
		List<Patient> listPatient = patientBO.getlistPatient();
		List<String> listColumn = patientBO.getlistColumn();

		session.getTransaction().commit();

		req.setAttribute(LIST_PATIENT_NAMES_ATT, listPatient);
		req.setAttribute(LIST_COLUMN_ATT, listColumn);
		req.setAttribute(LIST_TITLE_ATT, LIST_TITLE);
		if (listPatient.isEmpty()) {
			patientBO.setError("No patient found");
			req.setAttribute(PATIENT_LIST_ATT, patientBO);
		}

		this.getServletContext().getRequestDispatcher(PATIENT_JSP)
				.forward(req, resp);
	}

}
